using Microsoft.Phone.Controls.Properties;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
namespace Microsoft.Phone.Controls
{
	[TemplatePart(Name = "ItemsPresenterTranslateTransform", Type = typeof(TranslateTransform)), TemplatePart(Name = "ItemsPresenter", Type = typeof(ItemsPresenter)), TemplatePart(Name = "MultipleSelectionModeSummary", Type = typeof(TextBlock)), TemplatePart(Name = "ItemsPresenterHost", Type = typeof(Canvas)), TemplateVisualState(GroupName = "PickerStates", Name = "Normal"), TemplateVisualState(GroupName = "PickerStates", Name = "Highlighted"), TemplateVisualState(GroupName = "PickerStates", Name = "Disabled")]
	public class ListPicker : ItemsControl
	{
		private const string ItemsPresenterPartName = "ItemsPresenter";
		private const string ItemsPresenterTranslateTransformPartName = "ItemsPresenterTranslateTransform";
		private const string ItemsPresenterHostPartName = "ItemsPresenterHost";
		private const string MultipleSelectionModeSummaryPartName = "MultipleSelectionModeSummary";
		private const string BorderPartName = "Border";
		private const string PickerStatesGroupName = "PickerStates";
		private const string PickerStatesNormalStateName = "Normal";
		private const string PickerStatesHighlightedStateName = "Highlighted";
		private const string PickerStatesDisabledStateName = "Disabled";
		private const double NormalModeOffset = 4.0;
		private readonly DoubleAnimation _heightAnimation = new DoubleAnimation();
		private readonly DoubleAnimation _translateAnimation = new DoubleAnimation();
		private readonly Storyboard _storyboard = new Storyboard();
		private PhoneApplicationFrame _frame;
		private PhoneApplicationPage _page;
		private FrameworkElement _itemsPresenterHostParent;
		private Canvas _itemsPresenterHostPart;
		private ItemsPresenter _itemsPresenterPart;
		private TranslateTransform _itemsPresenterTranslateTransformPart;
		private bool _updatingSelection;
		private int _deferredSelectedIndex = -1;
		private object _deferredSelectedItem;
		private object _frameContentWhenOpened;
		private NavigationInTransition _savedNavigationInTransition;
		private NavigationOutTransition _savedNavigationOutTransition;
		private ListPickerPage _listPickerPage;
		private TextBlock _multipleSelectionModeSummary;
		private Border _border;
		private bool _hasPickerPageOpen;
		public static readonly DependencyProperty SummaryForSelectedItemsDelegateProperty = DependencyProperty.Register("SummaryForSelectedItemsDelegate", typeof(Func<IList, string>), typeof(ListPicker), null);
		public static readonly DependencyProperty ListPickerModeProperty = DependencyProperty.Register("ListPickerMode", typeof(ListPickerMode), typeof(ListPicker), new PropertyMetadata(ListPickerMode.Normal, new PropertyChangedCallback(ListPicker.OnListPickerModeChanged)));
		private static readonly DependencyProperty IsHighlightedProperty = DependencyProperty.Register("IsHighlighted", typeof(bool), typeof(ListPicker), new PropertyMetadata(false, new PropertyChangedCallback(ListPicker.OnIsHighlightedChanged)));
		public static readonly DependencyProperty SelectedIndexProperty = DependencyProperty.Register("SelectedIndex", typeof(int), typeof(ListPicker), new PropertyMetadata(-1, new PropertyChangedCallback(ListPicker.OnSelectedIndexChanged)));
		public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem", typeof(object), typeof(ListPicker), new PropertyMetadata(null, new PropertyChangedCallback(ListPicker.OnSelectedItemChanged)));
		private static readonly DependencyProperty ShadowItemTemplateProperty = DependencyProperty.Register("ShadowItemTemplate", typeof(DataTemplate), typeof(ListPicker), new PropertyMetadata(null, new PropertyChangedCallback(ListPicker.OnShadowOrFullModeItemTemplateChanged)));
		public static readonly DependencyProperty FullModeItemTemplateProperty = DependencyProperty.Register("FullModeItemTemplate", typeof(DataTemplate), typeof(ListPicker), new PropertyMetadata(null, new PropertyChangedCallback(ListPicker.OnShadowOrFullModeItemTemplateChanged)));
		private static readonly DependencyProperty ActualFullModeItemTemplateProperty = DependencyProperty.Register("ActualFullModeItemTemplate", typeof(DataTemplate), typeof(ListPicker), null);
		public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register("Header", typeof(object), typeof(ListPicker), null);
		public static readonly DependencyProperty HeaderTemplateProperty = DependencyProperty.Register("HeaderTemplate", typeof(DataTemplate), typeof(ListPicker), null);
		public static readonly DependencyProperty FullModeHeaderProperty = DependencyProperty.Register("FullModeHeader", typeof(object), typeof(ListPicker), null);
		public static readonly DependencyProperty ItemCountThresholdProperty = DependencyProperty.Register("ItemCountThreshold", typeof(int), typeof(ListPicker), new PropertyMetadata(5, new PropertyChangedCallback(ListPicker.OnItemCountThresholdChanged)));
		public static readonly DependencyProperty PickerPageUriProperty = DependencyProperty.Register("PickerPageUri", typeof(Uri), typeof(ListPicker), null);
		public static readonly DependencyProperty ExpansionModeProperty = DependencyProperty.Register("ExpansionMode", typeof(ExpansionMode), typeof(ListPicker), new PropertyMetadata(ExpansionMode.ExpansionAllowed, null));
		public static readonly DependencyProperty SelectionModeProperty = DependencyProperty.Register("SelectionMode", typeof(SelectionMode), typeof(ListPicker), new PropertyMetadata(0, new PropertyChangedCallback(ListPicker.OnSelectionModeChanged)));
		public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register("SelectedItems", typeof(IList), typeof(ListPicker), new PropertyMetadata(new PropertyChangedCallback(ListPicker.OnSelectedItemsChanged)));
		public event SelectionChangedEventHandler SelectionChanged;
		public Func<IList, string> SummaryForSelectedItemsDelegate
		{
			get
			{
				return (Func<IList, string>)base.GetValue(ListPicker.SummaryForSelectedItemsDelegateProperty);
			}
			set
			{
				base.SetValue(ListPicker.SummaryForSelectedItemsDelegateProperty, value);
			}
		}
		public ListPickerMode ListPickerMode
		{
			get
			{
				return (ListPickerMode)base.GetValue(ListPicker.ListPickerModeProperty);
			}
			private set
			{
				base.SetValue(ListPicker.ListPickerModeProperty, value);
			}
		}
		private bool IsHighlighted
		{
			get
			{
				return (bool)base.GetValue(ListPicker.IsHighlightedProperty);
			}
			set
			{
				base.SetValue(ListPicker.IsHighlightedProperty, value);
			}
		}
		public int SelectedIndex
		{
			get
			{
				return (int)base.GetValue(ListPicker.SelectedIndexProperty);
			}
			set
			{
				base.SetValue(ListPicker.SelectedIndexProperty, value);
			}
		}
		public object SelectedItem
		{
			get
			{
				return base.GetValue(ListPicker.SelectedItemProperty);
			}
			set
			{
				base.SetValue(ListPicker.SelectedItemProperty, value);
			}
		}
		public DataTemplate FullModeItemTemplate
		{
			get
			{
				return (DataTemplate)base.GetValue(ListPicker.FullModeItemTemplateProperty);
			}
			set
			{
				base.SetValue(ListPicker.FullModeItemTemplateProperty, value);
			}
		}
		public object Header
		{
			get
			{
				return base.GetValue(ListPicker.HeaderProperty);
			}
			set
			{
				base.SetValue(ListPicker.HeaderProperty, value);
			}
		}
		public DataTemplate HeaderTemplate
		{
			get
			{
				return (DataTemplate)base.GetValue(ListPicker.HeaderTemplateProperty);
			}
			set
			{
				base.SetValue(ListPicker.HeaderTemplateProperty, value);
			}
		}
		public object FullModeHeader
		{
			get
			{
				return base.GetValue(ListPicker.FullModeHeaderProperty);
			}
			set
			{
				base.SetValue(ListPicker.FullModeHeaderProperty, value);
			}
		}
		public int ItemCountThreshold
		{
			get
			{
				return (int)base.GetValue(ListPicker.ItemCountThresholdProperty);
			}
			private set
			{
				base.SetValue(ListPicker.ItemCountThresholdProperty, value);
			}
		}
		public Uri PickerPageUri
		{
			get
			{
				return (Uri)base.GetValue(ListPicker.PickerPageUriProperty);
			}
			set
			{
				base.SetValue(ListPicker.PickerPageUriProperty, value);
			}
		}
		public ExpansionMode ExpansionMode
		{
			get
			{
				return (ExpansionMode)base.GetValue(ListPicker.ExpansionModeProperty);
			}
			set
			{
				base.SetValue(ListPicker.ExpansionModeProperty, value);
			}
		}
		public SelectionMode SelectionMode
		{
			get
			{
				return (SelectionMode)base.GetValue(ListPicker.SelectionModeProperty);
			}
			set
			{
				base.SetValue(ListPicker.SelectionModeProperty, value);
			}
		}
		public IList SelectedItems
		{
			get
			{
				return (IList)base.GetValue(ListPicker.SelectedItemsProperty);
			}
			set
			{
				base.SetValue(ListPicker.SelectedItemsProperty, value);
			}
		}
		private static void OnListPickerModeChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
		{
			((ListPicker)o).OnListPickerModeChanged((ListPickerMode)e.get_OldValue(), (ListPickerMode)e.get_NewValue());
		}
		private void OnListPickerModeChanged(ListPickerMode oldValue, ListPickerMode newValue)
		{
			if (ListPickerMode.Expanded == oldValue)
			{
				if (this._page != null)
				{
					this._page.remove_BackKeyPress(new EventHandler<CancelEventArgs>(this.OnPageBackKeyPress));
					this._page = null;
				}
				if (this._frame != null)
				{
					this._frame.remove_ManipulationStarted(new EventHandler<ManipulationStartedEventArgs>(this.OnFrameManipulationStarted));
					this._frame = null;
				}
			}
			if (ListPickerMode.Expanded == newValue)
			{
				if (this._frame == null)
				{
					this._frame = (Application.get_Current().get_RootVisual() as PhoneApplicationFrame);
					if (this._frame != null)
					{
						this._frame.AddHandler(UIElement.ManipulationStartedEvent, new EventHandler<ManipulationStartedEventArgs>(this.OnFrameManipulationStarted), true);
					}
				}
				if (this._frame != null)
				{
					this._page = (this._frame.get_Content() as PhoneApplicationPage);
					if (this._page != null)
					{
						this._page.add_BackKeyPress(new EventHandler<CancelEventArgs>(this.OnPageBackKeyPress));
					}
				}
			}
			if (ListPickerMode.Full == oldValue)
			{
				this.ClosePickerPage();
			}
			if (ListPickerMode.Full == newValue)
			{
				this.OpenPickerPage();
			}
			this.SizeForAppropriateView(ListPickerMode.Full != oldValue);
			this.IsHighlighted = (ListPickerMode.Expanded == newValue);
		}
		private static void OnIsHighlightedChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
		{
			(o as ListPicker).OnIsHighlightedChanged();
		}
		private void OnIsHighlightedChanged()
		{
			this.UpdateVisualStates(true);
		}
		private static void OnIsEnabledChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
		{
			(o as ListPicker).OnIsEnabledChanged();
		}
		private void OnIsEnabledChanged()
		{
			this.UpdateVisualStates(true);
		}
		private static void OnSelectedIndexChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
		{
			((ListPicker)o).OnSelectedIndexChanged((int)e.get_OldValue(), (int)e.get_NewValue());
		}
		private void OnSelectedIndexChanged(int oldValue, int newValue)
		{
			if (base.get_Items().get_Count() > newValue && (0 >= base.get_Items().get_Count() || newValue >= 0))
			{
				if (base.get_Items().get_Count() != 0 || newValue == -1)
				{
					if (!this._updatingSelection)
					{
						this._updatingSelection = true;
						this.SelectedItem = ((-1 != newValue) ? base.get_Items().get_Item(newValue) : null);
						this._updatingSelection = false;
					}
					if (-1 != oldValue)
					{
						ListPickerItem listPickerItem = (ListPickerItem)base.get_ItemContainerGenerator().ContainerFromIndex(oldValue);
						if (listPickerItem != null)
						{
							listPickerItem.IsSelected = false;
						}
					}
					return;
				}
			}
			if (base.get_Template() != null || 0 > newValue)
			{
				throw new InvalidOperationException(Resources.InvalidSelectedIndex);
			}
			this._deferredSelectedIndex = newValue;
		}
		private static void OnSelectedItemChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
		{
			((ListPicker)o).OnSelectedItemChanged(e.get_OldValue(), e.get_NewValue());
		}
		private void OnSelectedItemChanged(object oldValue, object newValue)
		{
			if (base.get_Items() != null && base.get_Items().get_Count() != 0)
			{
				int num = (newValue != null) ? base.get_Items().IndexOf(newValue) : -1;
				if (-1 == num && 0 < base.get_Items().get_Count())
				{
					throw new InvalidOperationException(Resources.InvalidSelectedItem);
				}
				if (!this._updatingSelection)
				{
					this._updatingSelection = true;
					this.SelectedIndex = num;
					this._updatingSelection = false;
				}
				if (this.ListPickerMode != ListPickerMode.Normal)
				{
					this.ListPickerMode = ListPickerMode.Normal;
				}
				else
				{
					this.SizeForAppropriateView(false);
				}
				SelectionChangedEventHandler selectionChanged = this.SelectionChanged;
				if (selectionChanged != null)
				{
					IList list = (oldValue == null) ? new object[0] : new object[]
					{
						oldValue
					};
					IList list2 = (newValue == null) ? new object[0] : new object[]
					{
						newValue
					};
					selectionChanged.Invoke(this, new SelectionChangedEventArgs(list, list2));
				}
				return;
			}
			else
			{
				if (base.get_Template() != null)
				{
					throw new InvalidOperationException(Resources.InvalidSelectedItem);
				}
				this._deferredSelectedItem = newValue;
				return;
			}
		}
		private static void OnShadowOrFullModeItemTemplateChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
		{
			((ListPicker)o).OnShadowOrFullModeItemTemplateChanged();
		}
		private void OnShadowOrFullModeItemTemplateChanged()
		{
			base.SetValue(ListPicker.ActualFullModeItemTemplateProperty, this.FullModeItemTemplate ?? base.get_ItemTemplate());
		}
		private static void OnItemCountThresholdChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
		{
			((ListPicker)o).OnItemCountThresholdChanged((int)e.get_NewValue());
		}
		private void OnItemCountThresholdChanged(int newValue)
		{
			if (newValue < 0)
			{
				throw new ArgumentOutOfRangeException("ItemCountThreshold");
			}
		}
		private static void OnSelectionModeChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
		{
			((ListPicker)o).OnSelectionModeChanged((SelectionMode)e.get_NewValue());
		}
		private void OnSelectionModeChanged(SelectionMode newValue)
		{
			if (newValue != 1)
			{
				if (newValue != 2)
				{
					if (this._multipleSelectionModeSummary != null && this._itemsPresenterHostPart != null)
					{
						this._multipleSelectionModeSummary.set_Visibility(1);
						this._itemsPresenterHostPart.set_Visibility(0);
						return;
					}
					return;
				}
			}
			if (this._multipleSelectionModeSummary != null && this._itemsPresenterHostPart != null)
			{
				this._multipleSelectionModeSummary.set_Visibility(0);
				this._itemsPresenterHostPart.set_Visibility(1);
				return;
			}
		}
		private static void OnSelectedItemsChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
		{
			((ListPicker)o).OnSelectedItemsChanged((IList)e.get_OldValue(), (IList)e.get_NewValue());
		}
		private void OnSelectedItemsChanged(IList oldValue, IList newValue)
		{
			this.UpdateSummary(newValue);
			SelectionChangedEventHandler selectionChanged = this.SelectionChanged;
			if (selectionChanged != null)
			{
				IList list = new List<object>();
				if (oldValue != null)
				{
					IEnumerator enumerator = oldValue.GetEnumerator();
					try
					{
						while (enumerator.MoveNext())
						{
							object current = enumerator.get_Current();
							if (newValue == null || !newValue.Contains(current))
							{
								list.Add(current);
							}
						}
					}
					finally
					{
						IDisposable disposable = enumerator as IDisposable;
						if (disposable != null)
						{
							disposable.Dispose();
						}
					}
				}
				IList list2 = new List<object>();
				if (newValue != null)
				{
					IEnumerator enumerator2 = newValue.GetEnumerator();
					try
					{
						while (enumerator2.MoveNext())
						{
							object current2 = enumerator2.get_Current();
							if (oldValue == null || !oldValue.Contains(current2))
							{
								list2.Add(current2);
							}
						}
					}
					finally
					{
						IDisposable disposable2 = enumerator2 as IDisposable;
						if (disposable2 != null)
						{
							disposable2.Dispose();
						}
					}
				}
				selectionChanged.Invoke(this, new SelectionChangedEventArgs(list, list2));
			}
		}
		public ListPicker()
		{
			base.set_DefaultStyleKey(typeof(ListPicker));
			Storyboard.SetTargetProperty(this._heightAnimation, new PropertyPath(FrameworkElement.HeightProperty));
			Storyboard.SetTargetProperty(this._translateAnimation, new PropertyPath(TranslateTransform.YProperty));
			Duration duration = TimeSpan.FromSeconds(0.2);
			this._heightAnimation.set_Duration(duration);
			this._translateAnimation.set_Duration(duration);
			ExponentialEase exponentialEase = new ExponentialEase();
			exponentialEase.set_EasingMode(2);
			exponentialEase.set_Exponent(4.0);
			IEasingFunction easingFunction = exponentialEase;
			this._heightAnimation.set_EasingFunction(easingFunction);
			this._translateAnimation.set_EasingFunction(easingFunction);
			this.RegisterNotification("IsEnabled", new PropertyChangedCallback(ListPicker.OnIsEnabledChanged));
			base.add_Loaded(new RoutedEventHandler(this.OnLoaded));
			base.add_Unloaded(new RoutedEventHandler(this.OnUnloaded));
		}
		private void OnLoaded(object sender, RoutedEventArgs e)
		{
			this.UpdateVisualStates(true);
		}
		private void OnUnloaded(object sender, RoutedEventArgs e)
		{
			if (this._frame != null)
			{
				this._frame.remove_ManipulationStarted(new EventHandler<ManipulationStartedEventArgs>(this.OnFrameManipulationStarted));
				this._frame = null;
			}
		}
		public override void OnApplyTemplate()
		{
			if (this._itemsPresenterHostParent != null)
			{
				this._itemsPresenterHostParent.remove_SizeChanged(new SizeChangedEventHandler(this.OnItemsPresenterHostParentSizeChanged));
			}
			this._storyboard.Stop();
			base.OnApplyTemplate();
			this._itemsPresenterPart = (base.GetTemplateChild("ItemsPresenter") as ItemsPresenter);
			this._itemsPresenterTranslateTransformPart = (base.GetTemplateChild("ItemsPresenterTranslateTransform") as TranslateTransform);
			this._itemsPresenterHostPart = (base.GetTemplateChild("ItemsPresenterHost") as Canvas);
			this._itemsPresenterHostParent = ((this._itemsPresenterHostPart != null) ? (this._itemsPresenterHostPart.get_Parent() as FrameworkElement) : null);
			this._multipleSelectionModeSummary = (base.GetTemplateChild("MultipleSelectionModeSummary") as TextBlock);
			this._border = (base.GetTemplateChild("Border") as Border);
			if (this._itemsPresenterHostParent != null)
			{
				this._itemsPresenterHostParent.add_SizeChanged(new SizeChangedEventHandler(this.OnItemsPresenterHostParentSizeChanged));
			}
			if (this._itemsPresenterHostPart != null)
			{
				Storyboard.SetTarget(this._heightAnimation, this._itemsPresenterHostPart);
				if (!this._storyboard.get_Children().Contains(this._heightAnimation))
				{
					this._storyboard.get_Children().Add(this._heightAnimation);
				}
			}
			else
			{
				if (this._storyboard.get_Children().Contains(this._heightAnimation))
				{
					this._storyboard.get_Children().Remove(this._heightAnimation);
				}
			}
			if (this._itemsPresenterTranslateTransformPart != null)
			{
				Storyboard.SetTarget(this._translateAnimation, this._itemsPresenterTranslateTransformPart);
				if (!this._storyboard.get_Children().Contains(this._translateAnimation))
				{
					this._storyboard.get_Children().Add(this._translateAnimation);
				}
			}
			else
			{
				if (this._storyboard.get_Children().Contains(this._translateAnimation))
				{
					this._storyboard.get_Children().Remove(this._translateAnimation);
				}
			}
			DependencyProperty arg_1E7_1 = ListPicker.ShadowItemTemplateProperty;
			Binding binding = new Binding("ItemTemplate");
			binding.set_Source(this);
			base.SetBinding(arg_1E7_1, binding);
			if (-1 != this._deferredSelectedIndex)
			{
				this.SelectedIndex = this._deferredSelectedIndex;
				this._deferredSelectedIndex = -1;
			}
			if (this._deferredSelectedItem != null)
			{
				this.SelectedItem = this._deferredSelectedItem;
				this._deferredSelectedItem = null;
			}
			this.OnSelectionModeChanged(this.SelectionMode);
			this.OnSelectedItemsChanged(this.SelectedItems, this.SelectedItems);
		}
		protected override bool IsItemItsOwnContainerOverride(object item)
		{
			return item is ListPickerItem;
		}
		protected override DependencyObject GetContainerForItemOverride()
		{
			return new ListPickerItem();
		}
		protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
		{
			base.PrepareContainerForItemOverride(element, item);
			ContentControl contentControl = (ContentControl)element;
			contentControl.add_Tap(new EventHandler<GestureEventArgs>(this.OnContainerTap));
			contentControl.add_SizeChanged(new SizeChangedEventHandler(this.OnListPickerItemSizeChanged));
			if (object.Equals(item, this.SelectedItem))
			{
				this.SizeForAppropriateView(false);
			}
		}
		protected override void ClearContainerForItemOverride(DependencyObject element, object item)
		{
			base.ClearContainerForItemOverride(element, item);
			ContentControl contentControl = (ContentControl)element;
			contentControl.remove_Tap(new EventHandler<GestureEventArgs>(this.OnContainerTap));
			contentControl.remove_SizeChanged(new SizeChangedEventHandler(this.OnListPickerItemSizeChanged));
		}
		protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
		{
			base.OnItemsChanged(e);
			if (0 < base.get_Items().get_Count() && this.SelectedItem == null)
			{
				if (base.GetBindingExpression(ListPicker.SelectedIndexProperty) == null && base.GetBindingExpression(ListPicker.SelectedItemProperty) == null)
				{
					this.SelectedIndex = 0;
				}
			}
			else
			{
				if (base.get_Items().get_Count() == 0)
				{
					this.SelectedIndex = -1;
					this.ListPickerMode = ListPickerMode.Normal;
				}
				else
				{
					if (base.get_Items().get_Count() <= this.SelectedIndex)
					{
						this.SelectedIndex = base.get_Items().get_Count() - 1;
					}
					else
					{
						if (!object.Equals(base.get_Items().get_Item(this.SelectedIndex), this.SelectedItem))
						{
							int num = base.get_Items().IndexOf(this.SelectedItem);
							if (-1 == num)
							{
								this.SelectedItem = base.get_Items().get_Item(0);
							}
							else
							{
								this.SelectedIndex = num;
							}
						}
					}
				}
			}
			base.get_Dispatcher().BeginInvoke(delegate
			{
				this.SizeForAppropriateView(false);
			});
		}
		private bool IsValidManipulation(object OriginalSource, Point p)
		{
			DependencyObject dependencyObject = OriginalSource as DependencyObject;
			while (dependencyObject != null)
			{
				if (this._itemsPresenterHostPart != dependencyObject && this._multipleSelectionModeSummary != dependencyObject)
				{
					if (this._border != dependencyObject)
					{
						dependencyObject = VisualTreeHelper.GetParent(dependencyObject);
						continue;
					}
				}
				double num = 11.0;
				return p.get_X() > 0.0 && p.get_Y() > 0.0 - num && p.get_X() < this._border.get_RenderSize().get_Width() && p.get_Y() < this._border.get_RenderSize().get_Height() + num;
			}
			return false;
		}
		protected override void OnTap(GestureEventArgs e)
		{
			if (e == null)
			{
				throw new ArgumentNullException("e");
			}
			if (this.ListPickerMode == ListPickerMode.Normal)
			{
				if (!base.get_IsEnabled())
				{
					e.set_Handled(true);
					return;
				}
				Point position = e.GetPosition((UIElement)e.get_OriginalSource());
				if (this.IsValidManipulation(e.get_OriginalSource(), position) && this.Open())
				{
					e.set_Handled(true);
				}
			}
		}
		protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
		{
			if (e == null)
			{
				throw new ArgumentNullException("e");
			}
			base.OnManipulationStarted(e);
			if (this.ListPickerMode == ListPickerMode.Normal)
			{
				if (!base.get_IsEnabled())
				{
					e.Complete();
					return;
				}
				Point point = e.get_ManipulationOrigin();
				if (e.get_OriginalSource() != e.get_ManipulationContainer())
				{
					point = e.get_ManipulationContainer().TransformToVisual((UIElement)e.get_OriginalSource()).Transform(point);
				}
				if (this.IsValidManipulation(e.get_OriginalSource(), point))
				{
					this.IsHighlighted = true;
				}
			}
		}
		protected override void OnManipulationDelta(ManipulationDeltaEventArgs e)
		{
			if (e == null)
			{
				throw new ArgumentNullException("e");
			}
			base.OnManipulationDelta(e);
			if (this.ListPickerMode == ListPickerMode.Normal)
			{
				if (!base.get_IsEnabled())
				{
					e.Complete();
					return;
				}
				Point point = e.get_ManipulationOrigin();
				if (e.get_OriginalSource() != e.get_ManipulationContainer())
				{
					point = e.get_ManipulationContainer().TransformToVisual((UIElement)e.get_OriginalSource()).Transform(point);
				}
				if (!this.IsValidManipulation(e.get_OriginalSource(), point))
				{
					this.IsHighlighted = false;
					e.Complete();
				}
			}
		}
		protected override void OnManipulationCompleted(ManipulationCompletedEventArgs e)
		{
			if (e == null)
			{
				throw new ArgumentNullException("e");
			}
			base.OnManipulationCompleted(e);
			if (!base.get_IsEnabled())
			{
				return;
			}
			if (this.ListPickerMode == ListPickerMode.Normal)
			{
				this.IsHighlighted = false;
			}
		}
		public bool Open()
		{
			if (this.SelectionMode != null)
			{
				this.ListPickerMode = ListPickerMode.Full;
				return true;
			}
			if (this.ListPickerMode == ListPickerMode.Normal)
			{
				if (this.ExpansionMode == ExpansionMode.ExpansionAllowed && base.get_Items().get_Count() <= this.ItemCountThreshold)
				{
					this.ListPickerMode = ListPickerMode.Expanded;
				}
				else
				{
					this.ListPickerMode = ListPickerMode.Full;
				}
				return true;
			}
			return false;
		}
		private void OnItemsPresenterHostParentSizeChanged(object sender, SizeChangedEventArgs e)
		{
			if (this._itemsPresenterPart != null)
			{
				this._itemsPresenterPart.set_Width(e.get_NewSize().get_Width());
			}
			UIElement arg_48_0 = this._itemsPresenterHostParent;
			RectangleGeometry rectangleGeometry = new RectangleGeometry();
			rectangleGeometry.set_Rect(new Rect(default(Point), e.get_NewSize()));
			arg_48_0.set_Clip(rectangleGeometry);
		}
		private void OnListPickerItemSizeChanged(object sender, SizeChangedEventArgs e)
		{
			ContentControl contentControl = (ContentControl)sender;
			if (object.Equals(base.get_ItemContainerGenerator().ItemFromContainer(contentControl), this.SelectedItem))
			{
				this.SizeForAppropriateView(false);
			}
		}
		private void OnPageBackKeyPress(object sender, CancelEventArgs e)
		{
			this.ListPickerMode = ListPickerMode.Normal;
			e.set_Cancel(true);
		}
		private void SizeForAppropriateView(bool animate)
		{
			switch (this.ListPickerMode)
			{
			case ListPickerMode.Normal:
				this.SizeForNormalMode(animate);
				break;
			case ListPickerMode.Expanded:
				this.SizeForExpandedMode();
				break;
			}
			this._storyboard.Begin();
			if (!animate)
			{
				this._storyboard.SkipToFill();
			}
		}
		private void SizeForNormalMode(bool animate)
		{
			ContentControl contentControl = (ContentControl)base.get_ItemContainerGenerator().ContainerFromItem(this.SelectedItem);
			if (contentControl != null)
			{
				if (0.0 < contentControl.get_ActualHeight())
				{
					this.SetContentHeight(contentControl.get_ActualHeight() + contentControl.get_Margin().get_Top() + contentControl.get_Margin().get_Bottom() - 8.0);
				}
				if (this._itemsPresenterTranslateTransformPart != null)
				{
					if (!animate)
					{
						this._itemsPresenterTranslateTransformPart.set_Y(-4.0);
					}
					this._translateAnimation.set_To(new double?(contentControl.get_Margin().get_Top() - LayoutInformation.GetLayoutSlot(contentControl).get_Top() - 4.0));
					this._translateAnimation.set_From(animate ? default(double?) : this._translateAnimation.get_To());
				}
			}
			else
			{
				this.SetContentHeight(0.0);
			}
			ListPickerItem listPickerItem = (ListPickerItem)base.get_ItemContainerGenerator().ContainerFromIndex(this.SelectedIndex);
			if (listPickerItem != null)
			{
				listPickerItem.IsSelected = false;
			}
		}
		private void SizeForExpandedMode()
		{
			if (this._itemsPresenterPart != null)
			{
				this.SetContentHeight(this._itemsPresenterPart.get_ActualHeight());
			}
			if (this._itemsPresenterTranslateTransformPart != null)
			{
				this._translateAnimation.set_To(new double?(0.0));
			}
			ListPickerItem listPickerItem = (ListPickerItem)base.get_ItemContainerGenerator().ContainerFromIndex(this.SelectedIndex);
			if (listPickerItem != null)
			{
				listPickerItem.IsSelected = true;
			}
		}
		private void SetContentHeight(double height)
		{
			if (this._itemsPresenterHostPart != null && !double.IsNaN(height))
			{
				double height2 = this._itemsPresenterHostPart.get_Height();
				this._heightAnimation.set_From(new double?(double.IsNaN(height2) ? height : height2));
				this._heightAnimation.set_To(new double?(height));
			}
		}
		private void OnFrameManipulationStarted(object sender, ManipulationStartedEventArgs e)
		{
			if (ListPickerMode.Expanded == this.ListPickerMode)
			{
				DependencyObject dependencyObject = e.get_OriginalSource() as DependencyObject;
				DependencyObject dependencyObject2 = this._itemsPresenterHostPart ?? this;
				while (dependencyObject != null)
				{
					if (dependencyObject2 == dependencyObject)
					{
						return;
					}
					dependencyObject = VisualTreeHelper.GetParent(dependencyObject);
				}
				this.ListPickerMode = ListPickerMode.Normal;
			}
		}
		private void OnContainerTap(object sender, GestureEventArgs e)
		{
			if (ListPickerMode.Expanded == this.ListPickerMode)
			{
				ContentControl contentControl = (ContentControl)sender;
				this.SelectedItem = base.get_ItemContainerGenerator().ItemFromContainer(contentControl);
				this.ListPickerMode = ListPickerMode.Normal;
				e.set_Handled(true);
			}
		}
		private void UpdateVisualStates(bool useTransitions)
		{
			if (!base.get_IsEnabled())
			{
				VisualStateManager.GoToState(this, "Disabled", useTransitions);
				return;
			}
			if (this.IsHighlighted)
			{
				VisualStateManager.GoToState(this, "Highlighted", useTransitions);
				return;
			}
			VisualStateManager.GoToState(this, "Normal", useTransitions);
		}
		private void UpdateSummary(IList newValue)
		{
			string text = null;
			if (this.SummaryForSelectedItemsDelegate != null)
			{
				text = this.SummaryForSelectedItemsDelegate.Invoke(newValue);
			}
			if (text == null)
			{
				if (newValue != null && newValue.get_Count() != 0)
				{
					text = newValue.get_Item(0).ToString();
				}
				else
				{
					text = " ";
				}
			}
			if (string.IsNullOrEmpty(text))
			{
				text = " ";
			}
			if (this._multipleSelectionModeSummary != null)
			{
				this._multipleSelectionModeSummary.set_Text(text);
			}
		}
		private void OpenPickerPage()
		{
			if (null == this.PickerPageUri)
			{
				throw new ArgumentException("PickerPageUri");
			}
			if (this._frame == null)
			{
				this._frame = (Application.get_Current().get_RootVisual() as PhoneApplicationFrame);
				if (this._frame != null)
				{
					this._frameContentWhenOpened = this._frame.get_Content();
					UIElement uIElement = this._frameContentWhenOpened as UIElement;
					if (uIElement != null)
					{
						this._savedNavigationInTransition = TransitionService.GetNavigationInTransition(uIElement);
						TransitionService.SetNavigationInTransition(uIElement, null);
						this._savedNavigationOutTransition = TransitionService.GetNavigationOutTransition(uIElement);
						TransitionService.SetNavigationOutTransition(uIElement, null);
					}
					this._frame.add_Navigated(new NavigatedEventHandler(this.OnFrameNavigated));
					this._frame.add_NavigationStopped(new NavigationStoppedEventHandler(this.OnFrameNavigationStoppedOrFailed));
					this._frame.add_NavigationFailed(new NavigationFailedEventHandler(this.OnFrameNavigationStoppedOrFailed));
					this._hasPickerPageOpen = true;
					this._frame.Navigate(this.PickerPageUri);
				}
			}
		}
		private void ClosePickerPage()
		{
			if (this._frame != null)
			{
				this._frame.remove_Navigated(new NavigatedEventHandler(this.OnFrameNavigated));
				this._frame.remove_NavigationStopped(new NavigationStoppedEventHandler(this.OnFrameNavigationStoppedOrFailed));
				this._frame.remove_NavigationFailed(new NavigationFailedEventHandler(this.OnFrameNavigationStoppedOrFailed));
				UIElement uIElement = this._frameContentWhenOpened as UIElement;
				if (uIElement != null)
				{
					TransitionService.SetNavigationInTransition(uIElement, this._savedNavigationInTransition);
					this._savedNavigationInTransition = null;
					TransitionService.SetNavigationOutTransition(uIElement, this._savedNavigationOutTransition);
					this._savedNavigationOutTransition = null;
				}
				this._frame = null;
				this._frameContentWhenOpened = null;
			}
			if (this._listPickerPage != null)
			{
				if (this.SelectionMode == null && this._listPickerPage.SelectedItem != null)
				{
					this.SelectedItem = this._listPickerPage.SelectedItem;
				}
				else
				{
					if ((this.SelectionMode == 1 || this.SelectionMode == 2) && this._listPickerPage.SelectedItems != null)
					{
						this.SelectedItems = this._listPickerPage.SelectedItems;
					}
				}
				this._listPickerPage = null;
			}
		}
		private void OnFrameNavigated(object sender, NavigationEventArgs e)
		{
			if (e.get_Content() == this._frameContentWhenOpened)
			{
				this.ListPickerMode = ListPickerMode.Normal;
				return;
			}
			if (this._listPickerPage == null && this._hasPickerPageOpen)
			{
				this._hasPickerPageOpen = false;
				this._listPickerPage = (e.get_Content() as ListPickerPage);
				if (this._listPickerPage != null)
				{
					if (this.FullModeHeader != null)
					{
						this._listPickerPage.HeaderText = (string)this.FullModeHeader;
					}
					else
					{
						this._listPickerPage.HeaderText = (string)this.Header;
					}
					this._listPickerPage.FullModeItemTemplate = this.FullModeItemTemplate;
					this._listPickerPage.Items.Clear();
					if (base.get_Items() != null)
					{
						using (IEnumerator<object> enumerator = base.get_Items().GetEnumerator())
						{
							while (enumerator.MoveNext())
							{
								object current = enumerator.get_Current();
								this._listPickerPage.Items.Add(current);
							}
						}
					}
					this._listPickerPage.SelectionMode = this.SelectionMode;
					if (this.SelectionMode == null)
					{
						this._listPickerPage.SelectedItem = this.SelectedItem;
						return;
					}
					this._listPickerPage.SelectedItems.Clear();
					if (this.SelectedItems != null)
					{
						IEnumerator enumerator2 = this.SelectedItems.GetEnumerator();
						try
						{
							while (enumerator2.MoveNext())
							{
								object current2 = enumerator2.get_Current();
								this._listPickerPage.SelectedItems.Add(current2);
							}
						}
						finally
						{
							IDisposable disposable = enumerator2 as IDisposable;
							if (disposable != null)
							{
								disposable.Dispose();
							}
						}
					}
				}
			}
		}
		private void OnFrameNavigationStoppedOrFailed(object sender, EventArgs e)
		{
			this.ListPickerMode = ListPickerMode.Normal;
		}
	}
}
